package com.javaEE.tes.controllers;

import com.javaEE.tes.dao.dto.TeacherCurriculum;
import com.javaEE.tes.entities.Curriculum;
import com.javaEE.tes.entities.Teacher;
import com.javaEE.tes.services.CurriculumService;
import com.javaEE.tes.services.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;

@Controller
@RequestMapping("curriculum")
public class CurriculumControllers {
    @Autowired
    CurriculumService curriculumService;
    @Autowired
    TeacherService teacherService;
    @GetMapping
    public String displayCurriculum(Model model){
        List<TeacherCurriculum> curriculum= curriculumService.teacherCurricula();
        model.addAttribute("curriculum",curriculum);
        return "curriculum/list-curriculum";
    }
    @GetMapping("/new")
    public String displayCurriculumForm(Model model){
        Curriculum aCurriculum = new Curriculum();
        List<Teacher> teachers =teacherService.getAll();
        model.addAttribute("curriculum",aCurriculum);
        model.addAttribute("teacher",teachers);
        return "curriculum/new-curriculum";
    }
    @PostMapping("/save")
    public String createCurriculum(Model model, @Valid Curriculum curriculum, Errors errors){
        List<Teacher> teachers =teacherService.getAll();
        model.addAttribute("teacher",teachers);
        if (errors.hasErrors()) return "curriculum/new-curriculum";
        curriculumService.save(curriculum);
        return "redirect:/curriculum";
    }

}
